<?php
/**
 * 定义 Controller_BoProduct 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241027
 */

FLEA::loadClass('Controller_BoBase');
class Controller_BoUser extends Controller_BoBase
{
    var $_modelClasses;
    var $_modelUser;
    var $access;
    var $_helperFunction;
	var $dept_id;
	var $position_id;
	var $edu_id;
    var $dbo;
    
	//构造函数
    function Controller_BoUser() {
        parent::Controller_BoBase();
        $this->access = USER_ADMIN;
        //用户角色权限控制
        if(!$this->checkDispatcher($this->access)){
            redirect(url('BoDashboard','ErrorIn'));
        }
        $this->_modelClasses =& FLEA::getSingleton('Model_Categories');
        $this->_modelUser =& FLEA::getSingleton('Model_User');
		$this->dept_id = DEPARTMENT_CLASS;
		$this->position_id = POSITION_CLASS;
		$this->edu_id = EDUCATION_CLASS;
        $this->_helperFunction =& FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }
    
	//显示员工列表
    function actionIndex() {
		$field = array();
        if($_REQUEST['code'])$field['code'] = $_REQUEST['code'];
		if($_REQUEST['name'])$field[] = "name like '%".$_REQUEST['name']."%'";
        if($_REQUEST['position_id'])$field['position_id'] = $_REQUEST['position_id'];
		if($_REQUEST['dept_id'])$field['dept_id'] = $_REQUEST['dept_id'];
        if($_REQUEST['rstime'])$field[] = "employ_time > ".strtotime($_REQUEST['rstime'].' 00:00:00');
		if($_REQUEST['retime'])$field[] = "employ_time < ".strtotime($_REQUEST['retime'].' 00:00:00');
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $table =& $this->_modelUser->getTable();
        $pager =& new FLEA_Helper_Pager($table, $page, 15, $field, 'hot DESC,code asc,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $this->_setBack();
        $dept=$this->_modelClasses->getClass($this->dept_id);
        $deptclasses=$this->_modelClasses->getSubClasses($dept);
		$posi=$this->_modelClasses->getClass($this->position_id);
		$positions=$this->_modelClasses->getSubClasses($posi);
//        dump($this->dbo->log);
        $listTitle = "用户列表";
        include(TPL_DIR . '/userList.php');
    }  
     
    // 添加产品
    function actionCreate() {
        $table =& $this->_modelUser->getTable();
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }
    
    //保存
    function actionSave() {
        __TRY();
		if(!$_POST['uid'])$_POST['dateline'] = time();
		$_POST['updated'] = time();		
        $arid = $this->_modelUser->saveUser($_POST);        
//        dump($this->dbo->log);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
 	    	return $this->_editArticle($_POST, $ex->getMessage());
        }
        redirect($this->_url('Index'));
    }

    //修改文章
    function actionEdit() {
        $article = $this->_modelUser->getUser($_GET['id']);
        $this->_editArticle($article);
    }

	//显示文章信息编辑页面
    function _editArticle($article, $errorMessage = '') {
//    	var_dump($errorMessage);
    	$catid = $this->catid;
    	$table =& $this->_modelUser->getTable();
        $pk = $table->primaryKey;
        $dept=$this->_modelClasses->getClass($this->dept_id);
        $deptclasses=$this->_modelClasses->getSubClasses($dept);
		$posi=$this->_modelClasses->getClass($this->position_id);
		$positions=$this->_modelClasses->getSubClasses($posi);
		$edu=$this->_modelClasses->getClass($this->edu_id);
		$education=$this->_modelClasses->getSubClasses($edu);
		include(TPL_DIR . '/userEdit.php');
    }
    
    //删除资讯
    function actionRemove() {
        $this->_modelUser->removeUser($_GET['id']);
        $this->_goBack();
	}	
}
